package cn.jdy.plugins.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.framework.util.SpringUploadFile;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.CRateOrder;
import cn.jdy.plugins.service.CRateOrderService;


/**
 * @title 旅居订单管理后台操作
 * @author jga
 * @since 2018-09-21
 */
@Controller
@RequestMapping("/cRateOrder")
public class CRateOrderCtrl extends BaseCtrl{
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/CRateOrder/list?s=";
	
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(CRateOrder entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/cRateOrder/cRateOrderList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}

    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(CRateOrder sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", cRateOrderService.query(sp));
			jsonMap.put("iTotalDisplayRecords", cRateOrderService.count(sp));
			jsonMap.put("iTotalRecords", cRateOrderService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/cRateOrder/cRateOrderDetail.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.cRateOrderService.find(id));
		return mv;
	}
	
	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/save")
	 @ResponseBody
	public Map<String, Object> save(CRateOrder sp, String file) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(sp.getId()==null) {//
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			//	organid
				this.cRateOrderService.save(sp);
				mv.setViewName(this.viewName.concat("1"));
			} else {//更新
			sp.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.cRateOrderService.update(sp);
				mv.setViewName(this.viewName.concat("2"));
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return jsonMap;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.cRateOrderService.delete(id);
			 mv.setViewName(this.viewName.concat("3"));
	      // 清理内容页缓存
		} catch(Exception e) {
	     mv.setViewName(this.viewName.concat("4"));
	      BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}
}
